//package com.yuan.spring.redis;
//
//
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.stereotype.Component;
//
//import java.util.concurrent.TimeUnit;
//
///**
// * Created by 袁恩光 on 2017/12/12.
// */
//@Aspect
//@Component
//public class ConcurrentCheckAspect {
//
//    private static final Logger logger = LoggerFactory.getLogger(ConcurrentCheckAspect.class);
//
//    @Autowired
//    private RedisTemplate redisTemplate;
//
//    @Around("@annotation(concurrentCheck) || @within(concurrentCheck)")
//    public Object check(ProceedingJoinPoint joinPoint, ConcurrentCheck concurrentCheck) throws Throwable {
//        String className = joinPoint.getTarget().getClass().getSimpleName();
//        String methodName = ((MethodSignature) joinPoint.getSignature()).getMethod().getName();
//        concurrentCheck = ((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(ConcurrentCheck.class);
//        StringBuilder sb = new StringBuilder();
//        sb.append(className);
//        sb.append(methodName);
//        Object[] objects = joinPoint.getArgs();
//        for (Object object : objects) {
//            if (null != object) {
//                sb.append(String.valueOf(object));
//            }
//        }
//        String key = sb.toString();
//        logger.info("并发控制ConcurrentCheck");
//        if (!redisTemplate.opsForValue().setIfAbsent(key, key)) {
//            logger.info("已有正在处理的业务");
//            return "已有正在处理的业务";
//        } else {
//            redisTemplate.expire(key, concurrentCheck.timeOut(), TimeUnit.SECONDS);
//            Object object;
//            try {
//                object = joinPoint.proceed();
//            } finally {
//                redisTemplate.delete(key);
//            }
//            return object;
//        }
//    }
//}
